#include <iostream>
#include "epoll_server.h"
#include "socket_server.h"
#include "connect_func.h"

using namespace std;
using namespace Network_module;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "输入错误, please input: " << argv[0] << " + port" << endl;
        return 0;
    }

    uint16_t port = stoi(argv[1]);

    // 1.创建监听套接字(同时设置为非阻塞)
    unique_ptr<Tcp> _tcp_socket = make_unique<Tcp>(port);
    int listen_sockfd = _tcp_socket->Build_Listensocket();

    // 2.创建epoll服务器
    unique_ptr<Epoll_server> usv = make_unique<Epoll_server>();

    // 3.对listen套接字提供特定Accpter方法
    unique_ptr<ConnectFunc> pac = make_unique<ConnectFunc>();
    func_c listen_recver = bind(&ConnectFunc::AcceptConnect, pac.get(), placeholders::_1);

    // 4.构建listen对应的connection 交由Epoll_server进行管理
    Connection *pcon = Connection_factor::BuildListenConnection(
        listen_sockfd, EPOLLIN | EPOLLET, listen_recver, usv.get());

    usv->AddConnection(pcon);

    // 5.执行事件派发(epoll_wait事件就绪)
    usv->Run();

    return 0;
}